<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/11
 * Time: 1:23 AM
 */

namespace api\controllers;


use api\models\JobRecord;
use api\models\Mission;
use api\models\PartTimeJob;
use api\models\search\PartTimeSearch;
use api\models\User;
use yii\web\ServerErrorHttpException;

class PartTimeController extends BaseController
{
    public $modelClass = "\api\models\PartTimeJob";

    public function actions()
    {
        $actions          = parent::actions();
        $actions['index'] = [
            'class'               => 'yii\rest\IndexAction',
            'modelClass'          => $this->modelClass,
            'checkAccess'         => [$this, 'checkAccess'],
            'prepareDataProvider' => function ($action, $filter) {
                $model = new PartTimeSearch();

                return $model->search(\Yii::$app->request->get());
            }
        ];

        return $actions;

    }


    public function actionSignUpJob()
    { //已报名的兼职
        $model = PartTimeJob::find()->select(['job.*', 'job_record.uid'])
                            ->joinWith('jobInfo')
                            ->joinwith('u')
                            ->joinWith('jobRecords')
                            ->where([
                                'job_record.uid' => \Yii::$app->getUser()->id,
                                'type'           => PartTimeJob::PARTTIME_TYPE,
                                'forbidden'      => PartTimeJob::ACCESS,
                            ])
                            ->orderBy(['job.created_at' => SORT_DESC])
                            ->all();

        return $model;
    }

    public function actionPost()
    {
        $model = PartTimeJob::find()->select(['job.*'])
                            ->joinWith('jobInfo')
                            ->joinwith('u')
                            ->where([
                                'uid'       => \Yii::$app->getUser()->id,
                                'type'      => PartTimeJob::PARTTIME_TYPE,
                                'forbidden' => PartTimeJob::ACCESS,
                            ])
                            ->orderBy(['job.created_at' => SORT_DESC])
                            ->all();

        return $model;
    }


    public function actionPostUser($id = null)
    { //查看自己发布的兼职报名情况
        $res = [];
        if (isset($id)) {
            $users = User::find()
                         ->select(['user.*', 'job_record.jid'])
                         ->joinWith('records')
                         ->where([
                             'job_record.jid' => $id,
                             'user.status'    => User::STATUS_ACTIVE,
                         ])->all();
            $count = PartTimeJob::findOne($id)->view_num;
            $count = $count < 5 ? 5 : $count;//前五个免费
            foreach ($users as $user) {
                if ($count <= 0) {
                    break;
                }
                $count--;
                array_push($res, [
                    'uid'      => $user->id,
                    'username' => $user->username,
                    'phone'    => $user->phone
                ]);
            }
        }
        return $res;

    }


}